﻿// Ajax Loader
jQuery.fn.ajaxLoader = function (conf) {
    var config = jQuery.extend({
        className: 'jquery-ajax-loader',
        fadeDuration: 500
    }, conf);

    return this.each(function () {
        var t = jQuery(this);

        if (!this.ajaxLoaderObject) {
            var offset = t.offset();
            var dim = {
                left: offset.left,
                top: offset.top,
                width: t.outerWidth(),
                height: t.outerHeight()
            };

            this.ajaxLoaderObject = jQuery('<div class="' + config.className + '"></div>').css({
                position: 'absolute',
                left: dim.left + 'px',
                top: dim.top + 'px',
                width: dim.width + 'px',
                height: dim.height + 'px'
            }).appendTo(document.body).hide();
        }

        this.ajaxLoaderObject.fadeIn(config.fadeDuration);
    });
};

jQuery.fn.ajaxLoaderRemove = function () {
    return this.each(function () {
        if (this.ajaxLoaderObject) {
            this.ajaxLoaderObject.fadeOut(500);
        }
    });
};
//End of Ajax Loader

//Ajax Calling Method
function GetDataFromAjax(json, ajaxPage, loaderId) {
    jQuery('#' + loaderId).ajaxLoader();
    var options = {
        type: "POST",
        url: ajaxPage,
        data: json,
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        async: false,
        success: function (response) {
        },
        error: function (msg) { $('#ErrorMsg').html(msg); }
    };
    var returnText = jQuery.ajax(options).responseText;
    jQuery('#' + loaderId).ajaxLoaderRemove();
    return returnText;
}

//Comment Session Start Here
function GetAjaxLoader(json, ajaxPage, LoadingDivId) {
    jQuery('#' + LoadingDivId).ajaxLoader();
    var options = {
        type: "POST",
        url: ajaxPage,
        data: json,

        //        dataType: "json",
        async: false,
        success: function (response) {
        },
        error: function (msg) { alert("failed: " + msg); }
    };
    var returnText = $.ajax(options).responseText;
    jQuery('#' + LoadingDivId).ajaxLoaderRemove();
    return returnText;
}

function GetTemplate(handlerUrl, jsonData, resultDivId, templateId, PagingId) {
    var ajaxPage1 = handlerUrl;
    var returnText1 = GetAjaxLoader(jsonData, handlerUrl, 'Result');
    var v = $.evalJSON(returnText1);
    var total = v.total;
    var rows = v.rows;
    $("#" + resultDivId).html("");
    $('#' + PagingId).html(DoPaging(PageNo, total, PageSize));

    $.each(rows, function (s, k) {
        var kd = $("#" + templateId).html();
        var te = "";
        $.each(rows[s], function (k, v) {
            if (k == "DateStamp") {
                var milli = v.replace(/\/Date\((-?\d+)\)\//, '$1');
                var d = new Date(parseInt(milli));
                var c = $.format.date(d, "yyyy-MM-dd hh:mm a");
                kd = kd.replace("${" + k + "}", c);
            }
            else {
                kd = kd.replace("${" + k + "}", v);
            }
        });
        $("#" + resultDivId).append(kd);
    });
    return total;
}
var tools = {
    getStringBuilder: function () {
        var data = [];
        var counter = 0;
        return {
            // adds string s to the stringbuilder
            Append: function (s) { data[counter++] = s; return this; },
            // removes j elements starting at i, or 1 if j is omitted
            Remove: function (i, j) { data.splice(i, j || 1); return this; },
            // inserts string s at i
            Insert: function (i, s) { data.splice(i, 0, s); return this; },
            ToString: function (s) { return data.join(s || ""); return this; }
        };
    }
};
var StartPage;
var PageNo;
var PageSize = 10;
function LoadGridViewData(start, pageNo) {
    StartPage = start;
    PageNo = pageNo;
    var hd = jQuery("#hdId").val();
    var json = "{'startRowIndex':'" + StartPage + "','PageNo':'" + PageNo + "','PageSize':'" + PageSize + "','Id':'" + hd + "'}";

    GetTemplate("/Handlers/Comments.ashx?get=1", json, 'Result', 'template', 'paging');
}
function DoPaging(thisPageNo, totalCount, pageSize) {
    if (totalCount == 0) {
        return "";
    }

    var pageno = 0;
    var start = 0;
    var loop = totalCount / pageSize;
    var remainder = totalCount % pageSize;
    var startPageNoFrom = thisPageNo - 6;
    var endPageNoTo = thisPageNo + 6;
    var lastRenderedPageNo = 0;
    if (0 <= loop)
        return "";
    var strB = tools.getStringBuilder();
    strB.Append("<div>Page: ");

    // write 1st if required
    if (startPageNoFrom >= 1) {
        strB.Append("<a href=\"javascript:LoadGridViewData(0, 1)\" title=\"Page 1\">1</a> | ");
        if (!(startPageNoFrom == 1)) {
            strB.Append(" ... | ");
        }
    }

    for (i = 1; i < loop; i++) {
        pageno = i + 1;
        if (pageno > startPageNoFrom && pageno < endPageNoTo) {
            if (pageno == thisPageNo)
                strB.Append("<span>" + pageno + "</span>&nbsp;| ");
            else
                strB.Append("<a href=\"javascript:LoadGridViewData(" + start + ", " + pageno + ")\" title=\"Page " + pageno + "\">" + pageno + "</a> | ");

            lastRenderedPageNo = pageno;
        }
        start += pageSize;
    }

    // write ... if required just before end
    if (!(pageno == lastRenderedPageNo)) {
        strB.Append(" ... | ");
    }

    if (remainder > 0) {
        pageno++;
        if (pageno == thisPageNo)
            strB.Append("<span>" + pageno + "</span>&nbsp;| ");
        else
            strB.Append("<a href=\"javascript:LoadGridViewData(" + start + ", " + pageno + ")\" title=\"Page " + pageno + "\">" + pageno + "</a> | ");
    }
    else // write last page number
    {
        if (loop >= endPageNoTo) {
            if (loop == thisPageNo)
                strB.Append("<span>" + loop + "</span>&nbsp;| ");
            else
                strB.Append("<a href=\"javascript:LoadGridViewData(" + start + ", " + pageno + ")\" title=\"Page " + loop + "\">" + loop + "</a> | ");
        }
    }

    return strB.ToString() + "</div>";
}

// End here

$("#btnSubmit").click(function () {
    if ($("#txtName").val() == "") {
        alert("Please fill all blank fields");
        return false;
    }
    if ($("#txtDuration").val() == "") {
        alert("Please fill all blank fields");
        return false;
    }
    if ($("#ddlFormerType option:selected").val() == "0") {
        alert("Please fill all blank fields");
        return false;
    }
    var json = 'txtName=' + $("#txtName").val() + '&txtDuration=' + $("#txtDuration").val() + '&hdFormerId=' + $("#hdFormerId").val() + '&ddlFormerType=' + $("#ddlFormerType option:selected").val() + '&hdAction=' + $("#hdAction").val();
    //var json = "txtName='" + $("#txtName").val() + "',txtDuration:'" + $("#txtDuration").val() + "',hdFormerId:'" + $("#hdFormerId").val() + "'}";
    var value = GetAjaxLoader(json, "FormerInsert.php", 'MyFormerList');
    window.location = window.location;
});
$("#btnResult").click(function () {
    if ($("#txtName").val() == "") {
        alert("Please fill all blank fields");
        return false;
    }
    if ($("#txtDuration").val() == "") {
        alert("Please fill all blank fields");
        return false;
    }
    if ($("#ddlFormerType option:selected").val() == "0") {
        alert("Please fill all blank fields");
        return false;
    }
    var json = 'txtName=' + $("#txtName").val() + '&txtDuration=' + $("#txtDuration").val() + '&hdFormerId=' + $("#hdFormerId").val() + '&ddlFormerType=' + $("#ddlFormerType option:selected").val() + '&hdAction=' + $("#hdAction").val();
    //var json = "txtName='" + $("#txtName").val() + "',txtDuration:'" + $("#txtDuration").val() + "',hdFormerId:'" + $("#hdFormerId").val() + "'}";
    var value = GetAjaxLoader(json, "Election_ResultHelper.php", 'MyFormerList');
    window.location = window.location;
});
function FormerRemove(id) {
    var con = confirm("are you sure you want to delete?");
    if (con) {
        $("#hdAction").val("remove");
        var json = 'txtName=' + $("#txtName").val() + '&txtDuration=' + $("#txtDuration").val() + '&hdFormerId=' + $("#hdFormerId").val() + '&ddlFormerType=' + id + '&hdAction=' + $("#hdAction").val();
        //var json = "txtName='" + $("#txtName").val() + "',txtDuration:'" + $("#txtDuration").val() + "',hdFormerId:'" + $("#hdFormerId").val() + "'}";
        var value = GetAjaxLoader(json, "FormerInsert.php", 'MyFormerList');
        window.location = window.location;
    }
}
function ResultRemove(id) {
    var con = confirm("are you sure you want to delete?");
    if (con) {
        $("#hdAction").val("remove");
        var json = 'txtName=' + $("#txtName").val() + '&txtDuration=' + $("#txtDuration").val() + '&hdFormerId=' + $("#hdFormerId").val() + '&ddlFormerType=' + id + '&hdAction=' + $("#hdAction").val();
        //var json = "txtName='" + $("#txtName").val() + "',txtDuration:'" + $("#txtDuration").val() + "',hdFormerId:'" + $("#hdFormerId").val() + "'}";
        var value = GetAjaxLoader(json, "Election_ResultHelper.php", 'MyFormerList');
        window.location = window.location;
    }

}

if (document.getElementById('fileuploader') != null) {
    jQuery(function () {
        var uploader = new qq.FileUploader({
            element: document.getElementById('fileuploader'),
            action: 'upload.php',
            debug: true,
            async: false,
            allowedExtensions: ["png", "gif", "jpg"],
            multiple: false,

            onComplete: function (id, fileName, responseJSON) {

                var resultDiv = jQuery(".qq-upload-list");
                if (responseJSON.success) {
                    $("#txtDuration").val(responseJSON.url);
                }
            }
        });
    });
}

function linkResultGo() {
    window.location = "";
    window.location = "Election_Results.php?re=" + $("#ddlResultType option:selected").val();
}
function linkScheduleGo() {
    window.location = "";
    window.location = "Coming_elections.php?re=" + $("#ddlResultType option:selected").val();
}




//Schedule
$("#btnSedule").click(function () {

    if ($("#txtName").val() == "") {
        alert("Please fill all blank fields");
        return false;
    }
    if ($("#txtDuration").val() == "") {
        alert("Please fill all blank fields");
        return false;
    }
    if ($("#ddlFormerType option:selected").val() == "0") {
        alert("Please fill all blank fields");
        return false;
    }
    var json = 'txtName=' + $("#txtName").val() + '&txtDuration=' + $("#txtDuration").val() + '&hdFormerId=' + $("#hdFormerId").val() + '&ddlFormerType=' + $("#ddlFormerType option:selected").val() + '&hdAction=' + $("#hdAction").val();
    //var json = "txtName='" + $("#txtName").val() + "',txtDuration:'" + $("#txtDuration").val() + "',hdFormerId:'" + $("#hdFormerId").val() + "'}";
    var value = GetAjaxLoader(json, "Coming_Election_Helper.php", 'MyFormerList');
    window.location = window.location;
});
function ScheduleRemove(id) {
    var con = confirm("are you sure you want to delete?");
    if (con) {
        $("#hdAction").val("remove");
        var json = 'txtName=' + $("#txtName").val() + '&txtDuration=' + $("#txtDuration").val() + '&hdFormerId=' + $("#hdFormerId").val() + '&ddlFormerType=' + id + '&hdAction=' + $("#hdAction").val();
        //var json = "txtName='" + $("#txtName").val() + "',txtDuration:'" + $("#txtDuration").val() + "',hdFormerId:'" + $("#hdFormerId").val() + "'}";
        var value = GetAjaxLoader(json, "Coming_Election_Helper.php", 'MyFormerList');
        window.location = window.location;
    }
}

$("#btnVoter").click(function () {

    if ($("#txtName").val() == "") {
        alert("Please fill all blank fields");
        return false;
    }
    if ($("#txtDuration").val() == "") {
        alert("Please fill all blank fields");
        return false;
    }
    if ($("#ddlFormerType option:selected").val() == "0") {
        alert("Please fill all blank fields");
        return false;
    }
    var json = 'txtName=' + $("#txtName").val() + '&txtDuration=' + $("#txtDuration").val() + '&hdFormerId=' + $("#hdFormerId").val() + '&ddlFormerType=' + $("#ddlFormerType option:selected").val() + '&hdAction=' + $("#hdAction").val();
    //var json = "txtName='" + $("#txtName").val() + "',txtDuration:'" + $("#txtDuration").val() + "',hdFormerId:'" + $("#hdFormerId").val() + "'}";
    var value = GetAjaxLoader(json, "Election_VoterHelper.php", 'MyFormerList');
    window.location = window.location;
});
function VoterRemove(id) {
    var con = confirm("are you sure you want to delete?");
    if (con) {
        $("#hdAction").val("remove");
        var json = 'txtName=' + $("#txtName").val() + '&txtDuration=' + $("#txtDuration").val() + '&hdFormerId=' + $("#hdFormerId").val() + '&ddlFormerType=' + id + '&hdAction=' + $("#hdAction").val();
        //var json = "txtName='" + $("#txtName").val() + "',txtDuration:'" + $("#txtDuration").val() + "',hdFormerId:'" + $("#hdFormerId").val() + "'}";
        var value = GetAjaxLoader(json, "Election_VoterHelper.php", 'MyFormerList');
        window.location = window.location;
    }
}

function linkVoterListGo() {
    window.location = "";
    window.location = "Voters_List.php?re=" + $("#ddlResultType option:selected").val();
}

$("#btnExecutive").click(function () {

//    if ($.trim($("#txtName").val()) == "") {
//        alert("Please fill all blank fields");
//        return false;
//    }
//    if ($.trim($("#txtEmail").val()) == "") {
//        alert("Please fill all blank fields");
//        return false;
//    }
//    if ($.trim($("#txtDesignation").val()) == "") {
//        alert("Please fill all blank fields");
//        return false;
//    }
//    if ($.trim($("#txtCity").val()) == "") {
//        alert("Please fill all blank fields");
//        return false;
//    }
//    if ($.trim($("#txtPhone").val()) == "") {
//        alert("Please fill all blank fields");
//        return false;
//    }
//    if ($.trim($("#txtAddress").val()) == "") {
//        alert("Please fill all blank fields");
//        return false;
//    }
//    if ($.trim($("#txtDuration").val()) == "") {
//        alert("Please fill all blank fields");
//        return false;
//    }
    if ($("#ddlFormerType option:selected").val() == "0") {
        alert("Please fill all blank fields");
        return false;
    }
    if ($("#ddlCommiteeType option:selected").val() == "0") {
        alert("Please fill all blank fields");
        return false;
    }
    //    txtName=+asdf&txtEmail=+asdfas&txtDesignation=+asdf&txtCity=+asdf&txtPhone=+asdf&txtAddress=+asfdsadfdfs&hdAction=Add&txtDuration=uploads%2F4fca8b1b856a8.png&btnExecutive=Submit+&file=
    var json = 'txtName=' + $("#txtName").val() + '&txtDuration=' + $("#txtDuration").val() + '&txtEmail=' + $("#txtEmail").val() + '&ddlFormerType=' + $("#ddlFormerType option:selected").val() + '&hdAction=' + $("#hdAction").val();
    json = json + '&txtDesignation=' + $("#txtDesignation").val() + '&txtCity=' + $("#txtCity").val() + '&txtAddress=' + $("#txtAddress").val();
    json = json + '&txtDuration=' + $("#txtDuration").val() + '&txtPhone=' + $("#txtPhone").val() + '&ddlCommiteeType=' + $("#ddlCommiteeType").val();
    //    //var json = "txtName='" + $("#txtName").val() + "',txtDuration:'" + $("#txtDuration").val() + "',hdFormerId:'" + $("#hdFormerId").val() + "'}";
    var value = GetAjaxLoader(json, "ExecutiveHelper.php", 'MyFormerList');
    window.location = window.location;
});

function ExecutiveRemove(id) {
    var con = confirm("are you sure you want to delete?");
    if (con) {
        $("#hdAction").val("remove");
        var json = 'txtName=' + $("#txtName").val() + '&txtDuration=' + $("#txtDuration").val() + '&hdFormerId=' + $("#hdFormerId").val() + '&ddlFormerType=' + id + '&hdAction=' + "remove";
        //var json = "txtName='" + $("#txtName").val() + "',txtDuration:'" + $("#txtDuration").val() + "',hdFormerId:'" + $("#hdFormerId").val() + "'}";
        var value = GetAjaxLoader(json, "ExecutiveHelper.php", 'MyFormerList');
        window.location = window.location;
    }
}
function linkCenteralListGo() {

    window.location = pageName + "?re=" + $("#ddlResultType option:selected").val();
}
if (typeof dropdownId != 'undefined')
    $("#ddlResultType").val(dropdownId);